<?php
namespace app\home\controller;

use think\Validate;
use cmf\controller\HomeBaseController;
use app\home\model\MemberModel;
use think\Db;
use think\Image;
use cmf\lib\Storage;

class MemberController extends HomeBaseController
{
    //会员中心
    public function center(){
        //获取用户
        $user = cmf_get_current_user();
        $this->assign($user);
        $ergodic = new MemberModel();
        $list = $ergodic->MemberData();
        $this->assign("list",$list);
        return $this->fetch();
    }
    //用户编辑头像
    public function avatar(){
        $user = cmf_get_current_user();
        $this->assign($user);
        return $this->fetch();
    }
    // 用户头像上传
    public function avatarUpload()
    {
        $file   = $this->request->file('file');
        $result = $file->validate([
            'ext'  => 'jpg,jpeg,png',
            'size' => 1024 * 1024
        ])->move('.' . DS . 'upload' . DS . 'avatar' . DS);

        if ($result) {
            $avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
            $avatar         = 'avatar/' . $avatarSaveName;
            session('avatar', $avatar);

            return json_encode([
                'code' => 1,
                "msg"  => "上传成功",
                "data" => ['file' => $avatar],
                "url"  => ''
            ]);
        } else {
            return json_encode([
                'code' => 0,
                "msg"  => $file->getError(),
                "data" => "",
                "url"  => ''
            ]);
        }
    }
      // 用户头像裁剪
    public function avatarUpdate()
    {
        $avatar = session('avatar');
        if (!empty($avatar)) {
            $w = $this->request->param('w', 0, 'intval');
            $h = $this->request->param('h', 0, 'intval');
            $x = $this->request->param('x', 0, 'intval');
            $y = $this->request->param('y', 0, 'intval');

            $avatarPath = "./upload/" . $avatar;

            $avatarImg = Image::open($avatarPath);
            $avatarImg->crop($w, $h, $x, $y)->save($avatarPath);

            $result = true;
            if ($result === true) {
                $storage = new Storage();
                $result  = $storage->upload($avatar, $avatarPath, 'image');

                $userId = cmf_get_current_user();
                Db::name("member")->where(["m_guid" => $userId["m_guid"]])->update(["avatar" => $avatar]);
                session('user.avatar', $avatar);
                $this->success("头像更新成功！");
            } else {
                $this->error("头像保存失败！");
            }

        }
    }
    //编辑会员用户资料
    public function edit(){
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        return $this->fetch();
    }
    //编辑会员用户资料提交
    public function editPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $validate = new Validate([
                'nick_name' =>"require",
                'address' =>"require",
            ]);
            $validate->message([
                'nick_name'=>'用户名不能为空',
                'address' =>'地址不能为空',
            ]);
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $ergodic = new MemberModel();
            $result = $ergodic->MemberEdit($data);
            if($result){
                $this->success("修改成功!", "home/member/center");
            }else{
                $this->error("修改失败!","home/member/edit");
            }
        }else{
            $this->error("数据错误!");
        }
    }
    //修改会员密码
    public function password(){
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        return $this->fetch();
    }
    //会员用户修改密码提交
    public function passwordPost(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $validate = new Validate([
                'pass_word' =>"require",
                'pay_word' =>"require",
            ]);
            $validate->message([
                'pass_word'=>'登录密码不能为空',
                'pay_word' =>'提现不能为空',
            ]);
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $ergodic = new MemberModel();
            $result = $ergodic->MemberPassword($data);
            if($result){
                $this->success("修改成功!", "home/member/center");
            }else{
                $this->error("修改失败!","home/member/password");
            }
        }else{
            $this->error("数据错误!");
        }
    }
    //查询会员历史订单记录
    public function memberorder(){
        //显示会员昵称
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        $ergodic = new MemberModel();
        $list = $ergodic->MemberOrderOld();
        $this->assign("list",$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //在订单页面查询会员预约详情
    public function memberappment(){
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        $or_guid = $this->request->param("or_guid");
        $ergodic = new MemberModel();
        $list = $ergodic->MemberApption($or_guid);
        $this->assign("list",$list);
        return $this->fetch();
    }
    //会员查询个人所有的预约记录
    public function appmentlist(){
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        $ergodic = new MemberModel();
        $list = $ergodic->MemberAppListo($user);
        $this->assign("list",$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
    //会员卡查询
    public function membercardlist(){
        $user = cmf_get_current_user();
        $this->assign("user",$user);
        $ergodic = new MemberModel();
        $list = $ergodic->MemberCardXun($user);
        $this->assign("list",$list);
        $this->assign("page",$list);
        return $this->fetch();
    }
}
